/*
* Copyright (C) 2011 Jag Saund
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.vimana.widgets.progressbar;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;

/**
 * A simple activity used to test and demonstrate the progress bars
 * 
 * @author jsaund
 * 
 */
public class FancyProgressBarsActivity extends Activity {
  private SaundProgressBar m_miniProgressBar;
  private SaundProgressBar m_regularProgressBar;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    
    m_miniProgressBar = (SaundProgressBar) findViewById(R.id.miniprogressbar);
    m_regularProgressBar = (SaundProgressBar) findViewById(R.id.regularprogressbar);
    
    new UpdateBarTask().execute();
  }

  /**
   * A simple task that mimics a long running process and updates the progress
   * bar
   * 
   * @author jsaund
   * 
   */
  private class UpdateBarTask extends AsyncTask<Void, Integer, Void> {

    @Override
    protected Void doInBackground(Void... params) {
      int max = m_miniProgressBar.getMax();
      for (int i=0; i<=max; i++) {
        try {
          // update every second
          Thread.sleep(100);
        } catch(InterruptedException e) {
          
        }
        
        publishProgress(i);
      }
      
      return null;
    }
    
    @Override
    protected void onProgressUpdate(Integer... values) {
      m_miniProgressBar.setProgress(values[0]);
      m_regularProgressBar.setProgress(values[0]);
    }
  }
}